//
//  NSStringManager.m
//  KEREN
//
//  Created by MW on 2023/3/17.
//

#import "NSStringManager.h"

@implementation NSStringManager
+ (instancetype)shareInstance {
    static NSStringManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc]init];
    });
    return manager;
}
- (BOOL)judgePassWordLegal:(NSString *)pass {
    // 验证密码长度
    if(pass.length < 8 || pass.length > 16) {
        [[KRAlartManager shareInstance] showErrorAlert:@"密码由8-16位数字、字母或符号组成，至少含有两种字符以上"];
        return NO;
    }
    // 验证密码是否包含数字
    NSString *numPattern = @".*\\d+.*";
    NSPredicate *numPred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", numPattern];
    
    // 验证密码是否包含小写字母
    NSString *lowerPattern = @".*[a-z]+.*";
    NSPredicate *lowerPred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", lowerPattern];
    // 验证密码是否包含大写字母
    NSString *upperPattern = @".*[A-Z]+.*";
    NSPredicate *upperPred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", upperPattern];
    
    NSString *regex = @"[~`!@#$%^&*()_+-=[]|{};；‘。，！¥':\",./<>?]{,}/";



    NSInteger count = 0;
    if ([numPred evaluateWithObject:pass]) {
        count ++;
    }

    if ([lowerPred evaluateWithObject:pass] || [upperPred evaluateWithObject:pass]) {
        count ++;
    }
    for (int i = 0; i<pass.length; i++){
        NSString *string = [pass substringFromIndex:i];
        if ([regex containsString:string]) {
            count ++;
            break;
        }
    }

    if(count < 2){
        [[KRAlartManager shareInstance] showErrorAlert:@"密码由8-16位数字、字母或符号组成，至少含有两种字符以上"];
        return NO;
    }
    return YES;
}
- (BOOL)checkPhone:(NSString *)phone{
    if(phone.length < 1){
        [[KRAlartManager shareInstance] showErrorAlert:@"请输入正确手机号"];
        return NO;
    }
    NSString *firstStr = [phone substringToIndex:1];
    if(phone.length < 11 || ![firstStr isEqualToString:@"1"]) {
        [[KRAlartManager shareInstance] showErrorAlert:@"请输入正确手机号"];
        return NO;
    }
    return YES;
}

-(float)measureMutilineStringHeight:(NSString*)str andFont:(UIFont*)wordFont andWidthSetup:(float)width{
    
    if (str == nil || width <= 0) return 0;
    CGSize measureSize;
    if([[UIDevice currentDevice].systemVersion floatValue] < 7.0){
        measureSize = [str sizeWithFont:wordFont constrainedToSize:CGSizeMake(width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
    }else{
        measureSize = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:wordFont, NSFontAttributeName, nil] context:nil].size;
    }
    return ceil(measureSize.height);
}

- (float)measureSinglelineStringWidth:(NSString*)str andFont:(UIFont*)wordFont{
    if (str == nil) return 0;
    CGSize measureSize;
    if([[UIDevice currentDevice].systemVersion floatValue] < 7.0){
        measureSize = [str sizeWithFont:wordFont constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
    }else{
        measureSize = [str boundingRectWithSize:CGSizeMake(0, 0) options:NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:wordFont, NSFontAttributeName, nil] context:nil].size;
    }
    return ceil(measureSize.width);
}
- (CGSize)measureSinglelineStringSize:(NSString*)str andFont:(UIFont*)wordFont
{
    if (str == nil) return CGSizeZero;
    CGSize measureSize;
    if([[UIDevice currentDevice].systemVersion floatValue] < 7.0){
        measureSize = [str sizeWithFont:wordFont constrainedToSize:CGSizeMake(MAXFLOAT, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
    }else{
        measureSize = [str boundingRectWithSize:CGSizeMake(0, 0) options:NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:wordFont, NSFontAttributeName, nil] context:nil].size;
        
    }
    return measureSize;
}

@end
